Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ActiveRecord::StrictLoadingViolationError is not raised when loading polymorphic relations. #44906

Closed
madogiwa0124 opened this issue Apr 15, 2022 · 0 comments · Fixed by #45016
Assignees
Milestone

Comments

@madogiwa0124
Copy link
Contributor

Steps to reproduce

After performing strict_loading! on a model with polymorphic relationships, loading the related model raises an ArgumentError. However, it seems to me that ActiveRecord::StrictLoadingViolationError should be raised.

# frozen_string_literal: true

require "bundler/inline"

gemfile(true) do
  source "https://rubygems.org"

  git_source(:github) { |repo| "https://github.com/#{repo}.git" }

  # Activate the gem you are reporting the issue against.
  gem "activerecord", ENV.fetch('RAILS_VERSION',  "~> 7.0.0")
  gem "sqlite3"
end

require "active_record"
require "minitest/autorun"
require "logger"

# This connection will do for database-independent bug reports.
ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
ActiveRecord::Base.logger = Logger.new(STDOUT)

ActiveRecord::Schema.define do
  create_table :posts, force: true do |t|
  end

  create_table :comments, force: true do |t|
    t.belongs_to :target, polymorphic: true
  end
end

class Post < ActiveRecord::Base
  has_many :comments, as: :target
end

class Comment < ActiveRecord::Base
  belongs_to :target, polymorphic: true
end

class BugTest < Minitest::Test
  def test_strict_loading_to_polymofic_model
    post = Post.create!
    Comment.create!(target: post)
    comment = Comment.last
    comment.strict_loading!
    error = assert_raises(ArgumentError) { comment.target }
    assert_equal error.message, 'Polymorphic associations do not support computing the class.'
  end
end

Below is the repository from which the above reproduced code was pushed.

https://github.com/madogiwa0124/strict_loading_polymorphic_model_error_demo

Expected behavior

Raise ActiveRecord::StrictLoadingViolationError.

Actual behavior

Raise ArgumentError

ArgumentError: Polymorphic associations do not support computing the class.
  activerecord-7.0.2.3/lib/active_record/reflection.rb:417:in `compute_class'
  activerecord-7.0.2.3/lib/active_record/reflection.rb:376:in `klass'
  activerecord-7.0.2.3/lib/active_record/core.rb:241:in `strict_loading_violation!'
  activerecord-7.0.2.3/lib/active_record/associations/association.rb:220:in `find_target'
  activerecord-7.0.2.3/lib/active_record/associations/singular_association.rb:44:in `find_target'
  activerecord-7.0.2.3/lib/active_record/associations/association.rb:173:in `load_target'
  activerecord-7.0.2.3/lib/active_record/associations/association.rb:67:in `reload'
  activerecord-7.0.2.3/lib/active_record/associations/singular_association.rb:11:in `reader'
  activerecord-7.0.2.3/lib/active_record/associations/builder/association.rb:104:in `target'
  active_record_gem.rb:46:in `test_strict_loading_to_Polymorphic_model'

System configuration

Rails version: 6.1.5, 7.0.2.3

Ruby version: 2.7, 3.0, 3.1

Here are the reproduced verification results of the above combination in my repository

https://github.com/madogiwa0124/strict_loading_polymorphic_model_error_demo/actions/runs/2142890874

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants